package com.cobo.gen.blog;

import com.alibaba.fastjson.JSON;
import com.cobo.html.FileMeta;
import com.cobo.html.MatterEngine;
import okio.FileMetadata;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;

public class GenBlog {
    private String fp;

    private List<BlogMeta> blogMetas ;

    public GenBlog(String fp) {
        this.fp = fp;
        this.blogMetas = new ArrayList<>(250);
    }

    /**
     * ，分割的value值转为list
     * */
    private List<String> mapValToList(Map<String, String> map, String key) {
        if (map != null) {
            String val = map.get(key);
            if (val != null) {
                return Arrays.asList(val.split(","));
            }
        }
        return new ArrayList<>(0);
    }

    private String makePath(List<String> parentDir, String name) {
        return parentDir.stream().reduce((t1, t2) -> t1 + "/" + t2).orElse("") + "/" + StringUtils.substringBeforeLast(name, ".md");
    }


    private void invokeBlogMeta(String scanDir, List<String> parentDir) {
        File file = new File(scanDir);
        File[] innerFiles = file.listFiles();
        if (innerFiles != null) {
            for (File item : innerFiles) {
                try {
                    if (StringUtils.startsWith(item.getName(), ".")) {
                        //无视打头
                        continue;
                    }
                    if (item.isDirectory()) {
                        List<String> newCategory = new ArrayList<>(parentDir);
                        newCategory.add(item.getName());
                        invokeBlogMeta(item.getAbsolutePath(),newCategory);
                    }else {
                        if (item.getName().endsWith("md")) {
                            BlogMeta blogMeta = new BlogMeta();

                            MatterEngine matterEngine = new MatterEngine();
                            FileMeta matter = matterEngine.matter(FileUtils.readFileToString(item, "utf-8"));
                            Map<String, String> formatter = matter.getFormatter();
                            List<String> newCategory = new ArrayList<>(parentDir);
                            newCategory.addAll(mapValToList(formatter,"category"));
                            blogMeta.setCategory(new HashSet<>(newCategory));
                            blogMeta.setTags(new HashSet<>(mapValToList(formatter, "tag")));
                            blogMeta.setPath(makePath(parentDir, item.getName()));
                            blogMeta.setFilePath(item.getAbsolutePath());
                            blogMetas.add(blogMeta);
                        }

                    }
                }catch (Exception e){
                    e.printStackTrace();
                    System.err.println(item.getAbsolutePath() + "读取失败！");
                }
            }
        }
    }



    public void gen() {
        invokeBlogMeta(fp, new ArrayList<>());
        System.out.println(JSON.toJSONString(blogMetas));
    }



    public static void main(String[] args) {
        GenBlog genBlog = new GenBlog("D:\\person-doc");
        genBlog.gen();
    }
}
